package base

import (
	"data/config"
	"log"
	"public/message"
	"public/tools"
	"public/weixin"
	"strconv"
	"time"

	"github.com/ant0ine/go-json-rest/rest"
)

//获取签名信息
func GetSign(w rest.ResponseWriter, r *rest.Request) {
	var req GetSignParam
	tools.GetRequestJsonObj(r, &req)

	//入口参数检测
	if len(req.Url) == 0 {
		w.WriteJson(message.GetErrorMsg(message.ParameterInvalid))
		return
	}
	js_ticket, err := weixin.GetJsTicket()
	if err != nil {
		w.WriteJson(message.GetErrorMsg(message.UnknownError))
		return
	}
	log.Println("ttt", js_ticket)

	nonce_str := tools.GetRandomString(5)
	timestamp := strconv.FormatInt(time.Now().Unix(), 10)

	// var str = []string{nonce_str, timestamp, js_ticket, req.Url}
	// rank_s := tools.DictSort(str)     //按字典排序
	string1 := "jsapi_ticket=" + js_ticket + "&noncestr=" + nonce_str + "&timestamp=" + timestamp + "&url=" + req.Url
	log.Println("string1", string1)

	sign := tools.Sha1Encrypt(string1) //Sha1加密

	msg := message.GetSuccessMsg(message.NormalMessageId)
	mp := make(map[string]interface{})
	mp["nonce_str"] = nonce_str
	mp["timestamp"] = timestamp
	mp["sign"] = sign
	mp["app_id"] = config.GetWxInfo().AppID
	msg.Data = mp
	w.WriteJson(msg)
}
